/**
 * 栈的模拟实现
 */
import java.util.*;
public class MyStack<E>  {

    Object[] objects;
    int usedSize;//元素个数
    private static int initCapacity = 8;

    //默认初始化大小
    public MyStack(){
        objects = new Object[initCapacity];
    }

    //指定大小
    public MyStack(int capacity){
        if(capacity >= 0)
        objects = new Object[capacity];
    }

    public E push(Object val){

        if(usedSize == objects.length ) {
            //2倍扩容
            if (usedSize < 64) {
                objects = Arrays.copyOf(objects, objects.length * 2);
            } else {
                int newLength = (int)(objects.length * 1.5);
                objects = Arrays.copyOf(objects,newLength);
            }
        }

        //增加元素:坐标+1
        objects[usedSize++] = val;

        return (E)val;
    }

    public E pop(){

        if(size() == 0) return null;

        Object ret = objects[usedSize--];

        return (E)ret;
    }

    public E peek(){

        if(size() == 0) return null;

        return (E)(objects[usedSize]);
    }

    public int size(){
        return usedSize;
    }

    public boolean empty(){

        if(size() == 0) return true;

        else return false;
    }

    public static void main1(String[] args) {
          MyStack<Integer> myStack = new MyStack<>();

          //增加元素
        myStack.push(1);
        myStack.push(2);
        myStack.push(3);

        //判断是否为空
        System.out.println(myStack.empty());//false
        //获取大小
        int size = myStack.size();
        System.out.println(size);

        //出栈
        myStack.pop();
        myStack.pop();
        myStack.pop();

        System.out.println(myStack.empty());//true
    }
}
